import socket


def handle_request(client):
    buf = client.recv(1024).decode()
    # print(buf)
    print(buf)
    for s in str(buf).split('\r\n'):
        print(s)

    msg = "HTTP/1.1 2000 OK\r\n\r\n"  # HTTP头信息
    client.send(('%s' % msg).encode())
    msg = "<h1 style='color:red'>Hello, World! </h1>"
    client.send(('%s' % msg).encode())


def main():
    ip_port = ("localhost", 8000)
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind(ip_port)
    sock.listen(5)

    while True:
        conn, addr = sock.accept()
        handle_request(conn)
        conn.close()


if __name__ == "__main__":
    main()